import { TabsPage } from './../pages/tabs/tabs';
import { Component } from '@angular/core';
import { Platform, ToastController, App } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

import { HomePage } from '../pages/home/home';
@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  rootPage:any = TabsPage;
  exiting: boolean = false;

  constructor(
    private platform: Platform,
    statusBar: StatusBar,
    splashScreen: SplashScreen,
    private toast: ToastController,
    app: App
  ) {
    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      statusBar.styleDefault();
      splashScreen.hide();

      // 双击后退按钮退出
      platform.registerBackButtonAction((e) => {
        const overlay = app._appRoot._overlayPortal.getActive();
        const nav = app.getActiveNav();

        if (overlay && overlay.dismiss) {
          overlay.dismiss();
        } else if (nav.canGoBack()) {
          nav.pop();
        } else {
          this.showExit();
        }
      }, 101)
    });
  }


  showExit() {
    if (this.exiting === true) {
      this.platform.exitApp();
    } else {
      this.exiting = true;
      let tt = this.toast.create({
        message: '再按一次退出应用',
        duration: 2000,
        position: 'bottom'
      });
      tt.present();
      setTimeout(() => {
        this.exiting = false;
      }, 2000);
    }
  }
}

